package com.tanxiaoyao.wecon.ui;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tanxiaoyao.wecon.R;
import com.tanxiaoyao.wecon.app.AppManager;

import java.util.List;

/**
 * @author tanxiaoyao (junlidev@gmail.com)
 * @version 2014/9/1 15:15.
 */
public class ActivitySensorTest extends Activity {

	private View sensor_detail_header;
	private Button header_back_btn;
	private TextView header_title;
	private Button header_right_btn;
	private LinearLayout sensor_detail_ll;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Activity压栈
		AppManager.getAppManager().addActivity(this);
		setContentView(R.layout.activity_sensor_detail);
		// 得到标题栏View对象
		sensor_detail_header = findViewById(R.id.main_header);
		// 设置返回按钮属性
		header_back_btn = (Button) sensor_detail_header
				.findViewById(R.id.header_back_btn);
		header_back_btn.setText(R.string.back);
		header_back_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// 直接退出当前页面
				ActivitySensorTest.this.finish();
			}
		});
		// 设置标题栏文字
		header_title = (TextView) findViewById(R.id.header_title);
		header_title.setText(R.string.sensor);
		// 隐藏右边未用的按钮
		header_right_btn = (Button) findViewById(R.id.header_right_btn);
		header_right_btn.setVisibility(View.INVISIBLE);

		// 得到当前总显示区域
		sensor_detail_ll = (LinearLayout) findViewById(R.id.sensor_detail_ll);

		// 从系统服务中获得传感器管理器
		SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		// 从传感器管理器中获得全部的传感器列表
		List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL);
		// 依次打印每个传感器的信息
		for (Sensor s : allSensors) {
			// 首先得到本传感器的显示区域
			View view = this.getLayoutInflater().inflate(
					R.layout.sensor_detail_part, null);
			TextView title_text = (TextView) view
					.findViewById(R.id.sensor_detail_title);
			TextView content_text = (TextView) view
					.findViewById(R.id.sensor_detail_content);
			// 显示传感器名称
			switch (s.getType()) {
			case Sensor.TYPE_ACCELEROMETER:
				title_text.setText(R.string.type_accelerometer);
				break;
			case Sensor.TYPE_GRAVITY:
				title_text.setText(R.string.type_gravity);
				break;
			case Sensor.TYPE_GYROSCOPE:
				title_text.setText(R.string.type_gyroscope);
				break;
			case Sensor.TYPE_LIGHT:
				title_text.setText(R.string.type_light);
				break;
			case Sensor.TYPE_LINEAR_ACCELERATION:
				title_text.setText(R.string.type_linear_acceleration);
				break;
			case Sensor.TYPE_MAGNETIC_FIELD:
				title_text.setText(R.string.type_magnetic_field);
				break;
			case Sensor.TYPE_ORIENTATION:
				title_text.setText(R.string.type_orientation);
				break;
			case Sensor.TYPE_PRESSURE:
				title_text.setText(R.string.type_pressure);
				break;
			case Sensor.TYPE_PROXIMITY:
				title_text.setText(R.string.type_proximity);
				break;
			case Sensor.TYPE_TEMPERATURE:
				title_text.setText(R.string.type_temperature);
				break;
			case Sensor.TYPE_ROTATION_VECTOR:
				title_text.setText(R.string.type_rotation_vector);
				break;
			default:
				title_text.setText(R.string.othersensor);
				break;
			}
			// 显示传感器详情
			String curSensorInfo = "\n" + getString(R.string.sensor_name)
					+ "\t" + s.getName() + "\n"
					+ getString(R.string.sensor_version) + "\t"
					+ s.getVersion() + "\n"
					+ getString(R.string.sensor_factory) + "\t" + s.getVendor()
					+ "\n";
			content_text.setText(curSensorInfo);
			// 将控件添加到界面
			sensor_detail_ll.addView(view);
		}
	}

	@Override
	protected void onDestroy() {
		// Activity离栈
		AppManager.getAppManager().finishActivity(this);
		super.onDestroy();
	}
}